<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
      }
      ul {
        list-style: none;
      }
      .del {
        color: #ccc;
        cursor: pointer;
      }
      .main {
        max-width: 800px;
        margin: 0 auto;
        padding: 20px;
      }
      .pub-area {
        border-bottom: 4px solid rgb(255, 158, 13);
        margin-bottom: 20px;
      }
      .con-ipt {
        width: 100%;
        height: 140px;
        resize: none;
        border: 1px solid #119b04;
        border-radius: 10px;
        outline: none;
        padding: 10px;
        color: #666;
      }
      .row:nth-child(2) {
        display: flex;
        height: 80px;
        justify-content: flex-end;
        align-items: center;
      }
      .btn-pub {
        width: 120px;
        height: 40px;
        border: none;
        background-color: #119b04;
        border-radius: 6px;
        color: #fff;
        cursor: pointer;
      }
      .btn-pub:active {
        background-color: #0c6903;
      }
      .content-list {
        border-left: 2px solid #119b04;
        padding: 20px 0;
        padding-left: 40px;
      }
      .content-list li {
        border-bottom: 1px dashed #119b04;
      }
      .cotent {
        padding: 10px;
      }
      .info {
        padding: 10px;
        display: flex;
        justify-content: space-between;
      }
      .time {
        color: #666;
      }
    </style>
  </head>
  <body>
    <div class="main">
      <div class="pub-area">
        <div class="row">
          <textarea class="con-ipt" placeholder="记录下你的美好心情吧~"></textarea>
        </div>
        <div class="row">
          <button class="btn-pub">记录</button>
        </div>
      </div>
      <ul class="content-list">
        <!-- <li>
          <div class="cotent">这是一条心情</div>
          <div class="info">
            <span class="time">2021-01-01 12:33:33</span>
            <a href="#">删除</a>
          </div> 
        </li>-->
      </ul>
    </div>
    <script>
      var news = [
        { content: '翠花说今天去玩，没带上我，我很不开心。。。', time: '2020-01-01 23:59:59' },
        { content: '虽然翠花没带我去玩，但是给我带了礼物回来，好开心！！！', time: '2020-01-02 14:32:24' },
        { content: '翠花竟然不是只给我带了礼物，还给隔壁村二狗子也带了和我一样的礼物，生气！！！', time: '2020-01-04 16:00:24' }
      ]

      var html = ''
      news.forEach(e => {
        html += '<li>' + '<div class="cotent">' + e.content + '</div><div class="info">' + '<span class="time">' + e.time + '</span>' + '<span class="del">删除</span>' + '</div>' + '</li>'
      })

      var ul = document.querySelector('.content-list')
      ul.innerHTML = html

      var dels = document.querySelectorAll('.del')
      dels.forEach((e, i) => {
        e.onclick = removeContent
      })

      function removeContent() {
        if (!confirm('确定要删除吗')) return
        ul.removeChild(this.parentNode.parentNode)
      }

      var btn = document.querySelector('.btn-pub')
      var ipt = document.querySelector('.con-ipt')
      btn.onclick = function () {
        var content = ipt.value
        if (content.trim() === '') return
        var time = genTime()
        var item = genContentItem(content, time)
        ul.appendChild(item)
        ipt.value = null
      }

      function genContentItem(content, time) {
        var li = document.createElement('li')
        li.innerHTML = '<div class="cotent">' + content + '</div><div class="info">' + '<span class="time">' + time + '</span>' + '<span class="del">删除</span>' + '</div>'
        li.getElementsByClassName('del')[0].onclick = removeContent
        return li
      }

      function genTime() {
        var date = new Date()
        var y = date.getFullYear()
        var m = date.getMonth() + 1
        var d = date.getDate()
        var h = date.getHours()
        var mm = date.getMinutes()
        var s = date.getSeconds()
        return y + '-' + format(m) + '-' + format(d) + ' ' + format(h) + ':' + format(mm) + ':' + format(s)
      }
      function format(value) {
        return value < 10 ? '0' + value : value
      }
    </script>
  </body>
</html>
